/**
  * @desc check role required in request context
*/

const ResponseError = require('../helper/ResponseError')

const requireRequestContextRole = roles => (req, res, next) => {
  const validRoles = [].concat(roles)
  if (!validRoles.some(validRole => (req.context.role === validRole))) {
    return next(new ResponseError.InvalidPermission())
  }
  return next()
}

module.exports = requireRequestContextRole
